
export default class AnimationDemo{
    constructor(viewer){
        this.viewer =viewer;

    }
    loadMultipartVehicle(){
        let preloadTimeInSeconds = 100;
        let viewer =this.viewer;
        viewer.clock.shouldAnimate = true;
        let dataSource =new Cesium.CzmlDataSource();
        viewer.dataSources.add(dataSource);
        let partsPath ='/SampleData/';
        let vehicleEntity;
        var partsToLoad = [
            {
              url: "MultipartVehicle_part1.czml",
              range: [0, 1500],
              requested: false,
              loaded: false,
            },
            {
              url: "MultipartVehicle_part2.czml",
              range: [1500, 3000],
              requested: false,
              loaded: false,
            },
            {
              url: "MultipartVehicle_part3.czml",
              range: [3000, 4500],
              requested: false,
              loaded: false,
            },
          ];
          let statusDisplay =global.domUtil.createDivElement(['bgColor-div', 'infoPanel-left-top']);
          let fuelDisplay =global.domUtil.createDivElement(['bgColor-div', 'infoPanel-left-bottom'])


          processPart(partsToLoad[0]);
          this.viewer.clock.onTick.addEventListener(function(clock){
  
              let timeOffset =Cesium.JulianDate.secondsDifference(clock.currentTime,clock.startTime);
              partsToLoad.filter(function(part){
                  return(!part.requested && timeOffset >=part.range[0]-preloadTimeInSeconds && timeOffset <=part.range[1]);
              }).forEach(function(part){processPart(part)});
              if (vehicleEntity) {
                var fuel = vehicleEntity.properties.fuel_remaining.getValue(
                  clock.currentTime
                );
                if (Cesium.defined(fuel)) {
                  fuelDisplay.textContent = "Fuel: " + fuel.toFixed(2) + " gal";
                }
              }
          });

        function processPart(part){
            part.requested =true;
            updateStatusDisplay();
            dataSource.process(partsPath+part.url).then(function(){
                part.loaded =true;
                updateStatusDisplay();
                if(!viewer.trackedEntity){
                    vehicleEntity =dataSource.entities.getById("Vehicle");
                    viewer.trackedEntity =vehicleEntity;
                }
            })
        }


        function updateStatusDisplay() {
            var msg = "";
            partsToLoad.forEach(function (part) {
              msg += part.url + " - ";
              if (part.loaded) {
                msg += "Loaded.<br/>";
              } else if (part.requested) {
                msg += "Loading now...<br/>";
              } else {
                msg += "Not needed yet.<br/>";
              }
            });
            statusDisplay.innerHTML = msg;
          }
        // let fuelDisplay =document.createElement("div");
    }

    clearMultipartVehicle(){
        global.domUtil.clear();
        this.viewer.clock.shouldAnimate =false;
        this.viewer.trackedEntity =undefined;
        //删除entity和datasource等对象
    }
}